package com.study.example.study.extension.easyexcel.read;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import org.apache.commons.lang3.StringUtils;

/**
 * 空值转换
 *
 * @author RenYuLiang
 */
public class EasyExcelReadNullStrConverter implements Converter<String> {


    @Override
    public Class<?> supportJavaTypeKey() {
        // 对象属性类型
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        ExcelReader excelReader = EasyExcel.read("").build();

                EasyExcel.read("", null, null).doReadAll();
        // CellData 属性类型
        return CellDataTypeEnum.STRING;
    }

    /**
     * 这里是读的时候会调用
     *
     * @param context ReadConverterContext
     * @return Integer
     */
    @Override
    public String convertToJavaData(ReadConverterContext<?> context) {
        // CellData 转对象属性
        return context.getReadCellData().getStringValue();
    }

    /**
     * 这里是写的时候会调用
     *
     * @param context WriteConverterContext
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
        // 对象属性转 CellData
        String cellValue = context.getValue();
        String cellValueStr = "异常";
        if (StringUtils.isNotBlank(cellValue)) {
            cellValueStr = cellValue;
        }
        return new WriteCellData<>(cellValueStr);
    }

}
